package dataBase.magaers;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;

import servlets.ErrorCode;

import dataBase.ConnectionManager;
import dataBase.DBInitializer;
import dataBase.ISqlTable;

public class XsltDBManager {
	static int counter = 1;
	static private ISqlTable xslTable = null;
	static private ConnectionManager connectionManager = null;

	private static XsltDBManager instance = null;
	
	static public XsltDBManager getInstance(){
		if (instance == null){
			try {
				instance = new XsltDBManager();
			} catch (ClassNotFoundException e) {
				return null;
			}
		}
		return instance;
	}
	private XsltDBManager() throws ClassNotFoundException {
		xslTable = DBInitializer.getInstance().getXslTable();
		connectionManager = ConnectionManager.getInstance();
	}

	public boolean init(){
		 insertXsl(xsl1);
		 insertXsl(xsl2);
		 counter = getXslIds().size() + 1;
		 return true;
	}
	
	public String getXsl(String xslId) throws SQLException {
		if (xslId.equals("1"))
			return xsl1;
		if (xslId.equals("2"))
			return xsl2;
		
		ResultSet rs = null;
		String query = "SELECT xsl FROM " + xslTable.getName() + " WHERE XID='"
				+ xslId + "';";
		Statement stmt = connectionManager.getStatement(Connection.TRANSACTION_READ_COMMITTED);
		if (stmt == null) {
			return null;
		}
		try {
			rs = stmt.executeQuery(query);
			if (rs == null){
				return null;
			}
			rs.next();
			String res = (String) rs.getObject("xsl");
			if (!connectionManager.commitAndClose(stmt)) {
				return null;
			}
			return res;
		} catch (SQLException e) {
			connectionManager.rollbackAndClose(stmt);
			return null;
		}

	}	
	
	public boolean insertXsl(String toInsert){
		Statement stmt = connectionManager.getStatement(Connection.TRANSACTION_SERIALIZABLE);
		if (stmt == null){
			return false;
		}
		List<String> initialXsl = new LinkedList<String>();
		// insert first xsl
		initialXsl.add(String.valueOf(counter));
		counter++;
		initialXsl.add((XsltDBManager.xsl1).replaceAll("\\\"", "\\\\\""));
		try{
			xslTable.insertRecord(stmt, initialXsl);
			connectionManager.commitAndClose(stmt);
			return true;
		} catch (Exception e){
			connectionManager.rollbackAndClose(stmt);
			return false; //currently not using this return value
		}
	}
	
	public List<String> getXslIds(){
		Statement stmt = connectionManager.getStatement(Connection.TRANSACTION_READ_COMMITTED);
		if (stmt == null) {
			return null;
		}
		ResultSet rs = null;
		List<String> res = new LinkedList<String>();
		String query = "SELECT xid FROM " + xslTable.getName() + ";";
		try {
			rs = stmt.executeQuery(query);
			if (rs == null){
				return null;
			}
			while(rs.next()){
				res.add(String.valueOf((Integer)rs.getObject("xid")));
			}
			if (!connectionManager.commitAndClose(stmt)) {
				return null;
			}
			return res;
		} catch (SQLException e) {
			connectionManager.rollbackAndClose(stmt);
			return null;
		}
	}
	
	public final static String xsl1 = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>"
			+ "	<xsl:stylesheet version=\"1.0\""
			+ "		xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">"
			+ "		  <xsl:template match=\"/\">"
			+ "		    <html>"
			+ "				<head>"
			+ "					<title>Xsl design 1</title>"
			+ "				</head>"
			+ "		      <body>"
			+ "				<h1>Xsl design 1, detailed versions</h1>"
			+ "				<h3>Title:"
			+ "					<xsl:apply-templates select=\"//title\"/>"
			+ "				</h3>"
			+ "				<h3>Categories:</h3>"
			+ "				<ul>"
			+ "					<xsl:apply-templates select=\"//category\"/>"
			+ "				</ul>"
			+ "				<h3>Versions:</h3>"
			+ "				<table border=\"1\">"
			+ "				<tr>"
			+ "					<th>#</th>"
			+ "					<th>visibility</th>"
			+ "					<th>uid</th>"
			+ "					<th>language</th>"
			+ "					<th>view</th>"
			+ "				</tr>"
			+ "					<xsl:apply-templates select=\"//version\"/>"
			+ "				</table>"
			+ "				<div id=\"codeWindow\"></div>"
			+ "		      </body>"
			+ "		    </html>"
			+ "		  </xsl:template>"
			+ "			<xsl:template match=\"title\">"
			+ "				<xsl:value-of select=\".\" />"
			+ "		  </xsl:template>"
			+ "		  <xsl:template match=\"category\">"
			+ "				<li><xsl:value-of select=\".\" /></li>"
			+ "		  </xsl:template>"
			+ "		  <xsl:template match=\"version\">"
			+ "			<tr>"
			+ "				<td><xsl:value-of select=\"./@vernum\" /></td>"
			+ "				<td><xsl:value-of select=\"./@visible\" /></td>"
			+ "				<td><xsl:apply-templates select=\"./author\"/></td>"
			+ "				<td><xsl:apply-templates select=\"./code\"/></td>"
			+ "				<td><a href=\"site/edv.jsp?cid={./../@cid}&amp;version={./@vernum}\">show code</a></td>"
			+ "			</tr>"
			+ "		  </xsl:template>"
			+ "		  <xsl:template match=\"author\">"
			+ "				<xsl:value-of select=\"./@uid\" />"
			+ "		  </xsl:template>"
			+ "		  <xsl:template match=\"code\">"
			+ "				<xsl:value-of select=\"./@language\" />"
			+ "		  </xsl:template>" + "		</xsl:stylesheet>";

	public final static String xsl2 = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>"
			+ "<xsl:stylesheet version=\"1.0\" "
			+ "xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">"
			+ "  <xsl:template match=\"/\">"
			+ "    <html>"
			+ "		<head>"
			+ "			<title>Xsl design 2</title>"
			+ "		</head>"
			+ "      <body>"
			+ "		<h1>Xsl design 2, version summary:</h1>"
			+ "		<h3>The Document <xsl:apply-templates select=\"//title\"/> "
			+ "		has <xsl:value-of select=\"count(//version/code)\" /> versions.</h3>"
			+ "		<xsl:apply-templates select=\"//version\">"
			+ "		</xsl:apply-templates>"
			+ "      </body>"
			+ "    </html>"
			+ "	</xsl:template>"
			+ "	<xsl:template match=\"title\">"
			+ "		<xsl:value-of select=\".\" />"
			+ "	</xsl:template>"
			+ "	<xsl:template match=\"version[not(@vernum&lt;preceding-sibling::version/@vernum) and not(@vernum&lt;following-sibling::version/@vernum)]\">"
			+ "		<h4>The latest version is written in <xsl:value-of select=\"./code/@language\" /> language</h4>"
			+ "     <h4>Version content:</h4>"
			+ "		<xsl:value-of select=\"./code/.\" /><br />"
			+ "	</xsl:template>"
			+ "	<xsl:template match=\"version\" />"
			+ "  </xsl:stylesheet>";
}
